package com.itc.canvas;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDletStateChangeException;

import com.itc.dialog.AlertDialog;
import com.itc.display.Main;
import com.itc.resource.LoadResource;

public class MenuCanvas extends GameCanvas implements CommandListener {
	private Main midlet;
	private int width;
	private int height;
	private Command exitCommand;

	public MenuCanvas(Main midlet) {
		super(true);
		this.midlet = midlet;
		initViews();
	}

	public void initViews() {
		width = getWidth();
		height = getHeight();
		exitCommand = new Command("Exit", Command.EXIT, 1);
		addCommand(exitCommand);
		setCommandListener(this);
	}

	public void paint(Graphics g) {
		g.setColor(0x663399);
		g.fillRect(0, 0, width, height);
		g.drawImage(LoadResource.mImgLogo, (width - 160) / 2, 40, Graphics.TOP
				| Graphics.LEFT);

		g.drawImage(LoadResource.mImgPlay, (width - 80) / 2, 160, Graphics.TOP
				| Graphics.LEFT);

		g.drawImage(LoadResource.mImgAbout, (width - 50) / 2, 240, Graphics.TOP
				| Graphics.LEFT);

		g.drawImage(LoadResource.mImgHelp, (width - 50) / 2 - 70, 240,
				Graphics.TOP | Graphics.LEFT);

		g.drawImage(LoadResource.mImgExit, (width - 50) / 2 + 70, 240,
				Graphics.TOP | Graphics.LEFT);

		g.drawImage(LoadResource.mImgCopyRight, (width - 100) / 2, height - 20,
				Graphics.TOP | Graphics.LEFT);
	}

	protected void pointerPressed(int x, int y) {
		// Button play
		if (x >= (width - 80) / 2 && y >= 160 && x <= (width - 80) / 2 + 80
				&& y <= 160 + 40) {
			Display display = Display.getDisplay(this.midlet);
			GameUICanvas gameUICanvas = new GameUICanvas(this.midlet);
			display.setCurrent(gameUICanvas);
		}
		// Button about
		if (x >= (width - 50) / 2 && y >= 240 && x <= (width - 50) / 2 + 50
				&& y <= 240 + 26) {
			AlertDialog alertDialog = new AlertDialog();
			Alert confirmationAlert = alertDialog.createSetupAlert(null,
					"ABOUT",
					"\nFreaking Math\nVersion: 1.0.0\nDeveloped by ITC Group",
					AlertType.CONFIRMATION);
			Display display = Display.getDisplay(midlet);
			display.setCurrent(confirmationAlert);
		}
		// Button help
		if (x >= (width - 50) / 2 - 70 && y >= 240
				&& x <= (width - 50) / 2 - 70 + 50 && y <= 240 + 26) {
			AlertDialog alertDialog = new AlertDialog();
			Alert confirmationAlert = alertDialog.createSetupAlert(null,
					"HELP", "\n- You have only one second to choose correct answer\n- Reach high score!!!\n- Beat your friends!!!", AlertType.CONFIRMATION);
			Display display = Display.getDisplay(midlet);
			display.setCurrent(confirmationAlert);
		}
		// Button exit
		if (x >= (width - 50) / 2 + 70 && y >= 240
				&& x <= (width - 50) / 2 + 70 + 50 && y <= 240 + 26) {
			this.midlet.notifyDestroyed();
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {
			try {
				this.midlet.destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
